import service from '../service/BorrowMoneyService'
import LogUtil from '../utils/LogUtil';

export const NameSpace = 'LoanModel';

function createAction(action, payload) {
  return { type: `${NameSpace}/${action}`, payload };
}

export const Actions = {
  asyncActionGetBorrowMoney(params) {
    return createAction('asyncGetBoorMoney', params)
  },

  asyncActionNetGetContract(params) {
    return createAction('ansyContract', params)
  },
  asyncActionSetLoanInfo(params) {
    return createAction('asyncSetLoanInfo', params)
  },
};

function actionLoading(loading) {
  return createAction('loading', loading);
}


function actionList(list) {
  return createAction('list', list);
}
function actionLoanInfo(loan) {
  return createAction('loanInfo', loan);
}
function actionContract(contract) {
  return createAction('contract', contract)
}
function actionStat(stat) {
  return createAction('stat', stat)
}
function actionMsg(msg){
  return createAction('msg',msg)
}
export default {
  namespace: NameSpace,
  state: {
    loading: false,
    stat: 1,
    msg:'',
    list: null,
    loanInfo: {},
    contract: {}
  },
  reducers: {
    loading(state, { payload }) {
      return {
        ...state,
        loading: !state.list ? payload : false
      }
    },
    list(state, { payload }) {
      return {
        ...state,
        list: payload
      }
    },
    loanInfo(state, { payload }) {
      return {
        ...state,
        loanInfo: payload
      }
    },
    contract(state, { payload }) {
      return {
        ...state,
        contract: payload
      }
    },
    stat(state,{payload}){
      return {
        ...state,
        stat:payload
      }
    },
    msg(state,{payload}){
      return{
        ...state,
        msg:payload
      }
    }
  },
  asyncTasks: {
    asyncGetBoorMoney({ dispatch }, { payload }) {
      dispatch(actionLoading(true));
      service.netGetBorrowMoney(payload).then((res) => {
        switch(res.stat){
          case 1:
          dispatch(actionLoading(false));
          dispatch(actionList(res.info))
          dispatch(actionStat(1))
          break
          case 2:
          dispatch(actionLoading(false));
          dispatch(actionStat(2))
          dispatch(actionMsg(res.msg))
          break
          case 0:
          dispatch(actionLoading(false));
          dispatch(actionStat(0))
          dispatch(actionMsg(res.msg))
          break
          default:
          dispatch(actionLoading(false));
        }
      }).catch(() => {
        dispatch(actionLoading(false))

      })
    },

    asyncSetLoanInfo({ dispatch }, { payload }) {
      dispatch(actionLoanInfo(payload))
    },
    ansyContract({ dispatch }, { payload }) {
      service.netGetMoneyInfo(payload)
        .then((res) => {
          dispatch(actionContract(res))
          LogUtil.log('请求合同')
          LogUtil.log(typeof payload.id, typeof payload.day, typeof payload.money)
          LogUtil.log(payload)
          LogUtil.log(res)
        })
        .catch((err) => { })
    }
  }
}